package com.luo.locationapp.interfaces.impl;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.luo.locationapp.R;
import com.luo.locationapp.activity.MainMenuActivity;
import com.luo.locationapp.adapter.LocationMarkInfoAdapter;
import com.luo.locationapp.adapter.SideBarAdapter;
import com.luo.locationapp.bean.Bean_App_location_user;
import com.luo.locationapp.global.GlobalContents;
import com.luo.locationapp.utils.CustomDialog;
import com.luo.locationapp.utils.CustomJsonUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class ProcessorImplMethods {

	//��¼���ش���
	public void loginProcessor(Object updateView, String jsonString){
		Context context = (Context) updateView;
		String returnCode = CustomJsonUtils.getAttributeByName(jsonString, "returnCode");
		if("1".equals(returnCode)){
			CustomDialog.createCustomMessageDlg((Context) context, CustomJsonUtils.getAttributeByName(jsonString, "currentUser"),null);
			return;
		}
		String userInfoJson = CustomJsonUtils.getAttributeByName(jsonString, "currentUser");
		//��ǰ�û�����
		Bean_App_location_user currentUser = new Bean_App_location_user();
		currentUser.setId(CustomJsonUtils.getAttributeByName(userInfoJson, "id"));
		currentUser.setName(CustomJsonUtils.getAttributeByName(userInfoJson, "name"));
		currentUser.setPassword(CustomJsonUtils.getAttributeByName(userInfoJson, "password"));
		currentUser.setCreatetime(CustomJsonUtils.getAttributeByName(userInfoJson, "createtime"));
		currentUser.setComments(CustomJsonUtils.getAttributeByName(userInfoJson, "comments"));

		GlobalContents.userInfoMap.put("currentUser", currentUser);

		// ��ת
		context.startActivity(new Intent(context, MainMenuActivity.class));
	}
	
	//��ȡ���˵����ش���
	public void getMainMenuProcessor(Object updateView, String jsonString){
		ListView updateListView = (ListView) updateView;
		SideBarAdapter updateAdapter = (SideBarAdapter) updateListView.getAdapter();
		JSONArray menuArray = CustomJsonUtils.getArrayByName(jsonString, "menulist");
		for(int i = 0;i<menuArray.length();i++){
			try {
				JSONObject jo = (JSONObject) menuArray.get(i);
				Map<String,String> map = new HashMap<String, String>();
	        	map.put("image", jo.getString("image"));
	        	map.put("title", jo.getString("title"));
	        	map.put("activityUri", jo.getString("activityuri"));
	        	updateAdapter.addItem(i, map);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		updateAdapter.notifyDataSetChanged();
	}
	
	//��ȡ��ʷ�㼣��Ϣ
	public void getMarkedLocationInfoProcessor(Object updateView, String jsonString){
		ListView updateListView = (ListView) updateView;
		LocationMarkInfoAdapter updateAdapter = (LocationMarkInfoAdapter) updateListView.getAdapter();
		JSONArray menuArray = CustomJsonUtils.getArrayByName(jsonString, "markinfoList");
		for(int i = 0;i<menuArray.length();i++){
			try {
				JSONObject jo = (JSONObject) menuArray.get(i);
				Map<String,String> map = new HashMap<String, String>();
	        	map.put("marktime", jo.getString("marktime"));
	        	map.put("markstate", jo.getString("markstate"));
	        	map.put("locationinfo", jo.getString("locationinfo"));
	        	map.put("markimage", jo.getString("markimage"));
	        	updateAdapter.addItem(i, map);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		updateAdapter.notifyDataSetChanged();
	}

	//��ȡλ�ýӿڷ��ش���
	public void getLocationInfoProcessor(Object updateView, String jsonString){
		TextView tv = (TextView) updateView;
		tv.setText(jsonString);
	}
	
	public void processUploadProcessor(Object updateView, String jsonString){
		Activity activity = (Activity) updateView;
		String returnCode = CustomJsonUtils.getAttributeByName(jsonString, "returnCode");
		if("0".equals(returnCode))
			activity.finish();
		else{
			CustomDialog.createCustomMessageDlg(activity, CustomJsonUtils.getAttributeByName(jsonString, "message"), null);
		}
	}
	//注册
	public void registerProcessor(Object updateView, String jsonString){
		final Activity activity = (Activity) updateView;
		String returnCode = CustomJsonUtils.getAttributeByName(jsonString, "returnCode");
		if("0".equals(returnCode)){
			CustomDialog.createCustomMessageDlg(activity, CustomJsonUtils.getAttributeByName(jsonString, "message")
					+"\n账号："+CustomJsonUtils.getAttributeByName(jsonString, "acount")
					+"\n密码："+CustomJsonUtils.getAttributeByName(jsonString, "password")
					, new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					activity.finish();
				}
			});
		}else{
			CustomDialog.createCustomMessageDlg(activity, CustomJsonUtils.getAttributeByName(jsonString, "message"), null);
		}
	}
}
